package com.cloudfast.oauth.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import com.cloudfast.common.user.LoginUser;
import com.cloudfast.context.error.CloudException;
import com.cloudfast.context.error.ErrorCode;
import com.cloudfast.oauth.feign.UserCenterFeignService;
import com.cloudfast.oauth.principal.Principal;
import com.cloudfast.platform.menu.entity.Menu;
import com.cloudfast.platform.role.entity.Role;

/**
 * 查询数据库用户
 *
 * @author liuyw
 * @date 2021-11-16
 */
@Service
@Component
public class CustomUserDetailsService implements UserDetailsService {
    @Autowired
    private UserCenterFeignService userFeignService;

    @Override
    public UserDetails loadUserByUsername(String username) {
        List<GrantedAuthority> grantedAuthorities = new ArrayList<>();
        LoginUser loginUser = userFeignService.findByLoginName(username);

        if (loginUser != null) {
            // 得到机构编码-作为权限
            String orgCode = loginUser.getOrgCode();
            if (StringUtils.isNotEmpty(orgCode)) {
                grantedAuthorities.add(new SimpleGrantedAuthority(orgCode));
            }
            // 行政区域作为权限
            if (loginUser.getRegionCode() != null) {
                grantedAuthorities.add(new SimpleGrantedAuthority(loginUser.getRegionCode() + ""));
            }
            // 职能权限
            if (StringUtils.isNotEmpty(loginUser.getRankCode())) {
                grantedAuthorities.add(new SimpleGrantedAuthority(loginUser.getRankCode()));
            }

            // 权限角色编码-作为权限
            List<Role> roleList = userFeignService.findRoleCodeByUserId(loginUser.getId().toString());
            for (Role role : roleList) {
                if (StringUtils.isNotEmpty(role.getCode())) {
                    grantedAuthorities.add(new SimpleGrantedAuthority(role.getCode()));
                }
            }
            // 查询菜单权限
            List<Menu> menuAuthList = this.userFeignService.findPermissionByUserId(loginUser.getId().toString());
            for (Menu menu : menuAuthList) {
                if (StringUtils.isNotEmpty(menu.getCheckCode())) {
                    grantedAuthorities.add(new SimpleGrantedAuthority(menu.getCheckCode()));
                }
            }

            // 获取到按钮
            List<Menu> screenButtons = userFeignService.screenButtons(loginUser.getId());
            List<Map<String, Object>> systemMenus = userFeignService.userHasMenus("System", loginUser.getId());
            List<Map<String, Object>> portalMenus = userFeignService.userHasMenus("Portal", loginUser.getId());

            return new Principal(loginUser, grantedAuthorities, systemMenus, portalMenus, screenButtons);

        } else {
            throw new CloudException(ErrorCode.USERNAME_PASSWORD_ERROR);
        }
    }

}
